@extends('admin.app')

@section('content')
  <div class="panel panel-default">
    <div class="panel-heading font-bold">
      添加菜单表单
    </div>
    <div class="panel-body">
      <form class="form-horizontal form-validation" action="{{ admin_action('System\MenusController@store') }}" method="post">
        {{ csrf_field() }}
        <div class="form-group">
          <label class="col-lg-2 control-label">名称*</label>
          <div class="col-lg-10">
            <input type="text" class="form-control" name="name" value="{{ old('name') }}" placeholder="请输入名称" data-rule-required="true" data-rule-maxlength="20"> 
          </div>
        </div>
        <div class="line line-dashed b-b line-lg"></div>
        <div class="form-group">
          <label class="col-lg-2 control-label">控制器*</label>
          <div class="col-lg-10">
            <input type="text" class="form-control" name="action" value="{{ old('action') }}" placeholder="请输入控制器" data-rule-required="true" data-rule-maxlength="200"> 
          </div>
        </div>
        <div class="line line-dashed b-b line-lg"></div>
        <div class="form-group">
          <label class="col-lg-2 control-label">父级菜单</label>
          <div class="col-lg-2 select-box" ajax="{{ admin_action('System\MenusController@getChildren') }}">
          	<select name="pid[]" class="form-control">
          		<option value="">无</option>
              @foreach ($parents as $parent)
              <option value="{{ $parent->id }}" {{ old('pid') == $parent->id ? 'selected' : '' }}>{{ $parent->name }}</option>
              @endforeach
          	</select>
          </div>
        </div>
        <div class="line line-dashed b-b line-lg"></div>
        <div class="form-group">
          <label class="col-lg-2 control-label">描述</label>
          <div class="col-lg-10">
            <textarea class="form-control" rows="4" name="description" placeholder="请输入描述信息" data-rule-maxlength="100">{{ old('description') }}</textarea>              
          </div>
        </div>
        <div class="line line-dashed b-b line-lg"></div>
        <div class="form-group">
          <label class="col-lg-2 control-label">图标</label>
          <div class="col-lg-10">
            <input type="text" class="form-control" name="icon" value="{{ old('icon') ?? '' }}" placeholder="请输入图标" data-rule-maxlength="20"> 
          </div>
        </div>
        <div class="line line-dashed b-b line-lg"></div>
        <div class="form-group">
          <div class="col-lg-4 col-lg-offset-2">
            <a href="{{ admin_action('System\MenusController@index') }}" class="btn btn-default">返&nbsp;回</a>
            <button type="submit" class="btn btn-primary">保&nbsp;存</button>
          </div>
        </div>
      </form>
    </div>
  </div>
@endsection
@section('scripts')
<script type="text/javascript">
$(function(){
  var clone_select=true;
  $(".select-box select").on('change',function(){
    if(!clone_select){return;}
    clone_select=false;
    var parent=$(this).parent();
    parent.nextAll().remove();
    var id=$(this).val();
    var clone=parent.clone(true);
    if(id){
      $.get(parent.attr("ajax"),{id:id}).success(function(data){
        data=eval("("+data+")");
        if(data.length>0){
          clone.find("select").html("").append('<option value="">无</option>');
          for(var i in data){
            clone.find("select").append('<option value="'+data[i]['id']+'">'+data[i]['name']+'</option>');
          }
          parent.after(clone);
        }
        clone_select=true;
      });
    }
  });
})
</script>
@endsection